import Server from "@/config/server";
import request from '@/utils/request';
import { showErrors, showError, showSuccess } from '@/utils/showMsg';

const state = {
  requestCode: false,
  dataLists: {},
  Fields: [],
  itemDetail: {},
  groupData: [],

  dataPlatFormLists: {},
  PlatFormFields: [],
  dataAgentLists: {},
  AgentFields: [],
  dataCompanyLists: {},
  CompanyFields: [],

  userPlatformAll: [],
  agentAllData: [],
  companyAllData: [],
};
const actions = {
  //列表 （旧版）
  getLists({ commit }, page) {
    request.get(Server.action.group_list, { params: page })
      .then(response => {
        console.log(response);
        if (response.data.errorCode === 0) {
          commit("setLists", response.data.result.list);
          let fields = response.data.result.fields;
          fields.push({
            title: '操作',
            align: 'center',
            width: 210,
            scopedSlots: { customRender: 'action' },
            fixed: 'right',
          })
          fields[0].width = 80
          fields.forEach(function (item, i) {
            fields[i].dataIndex = fields[i].key
            fields[i].align = 'center';
            if (i < fields.length - 4) {
              item.width = item.width || 100
            }
            if (item.key === "address") {
              item.width = 200
            }
            if (item.key === "describe") {
              item.width = 200
            }
          })
          commit("setFields", fields);
        } else {

        }
      })
  },
  //添加
  dataAdd({ commit }, page) {
    commit("setErrorCode", false);
    request.post(Server.action.group_add, page)
      .then(response => {
        console.log(response);
        commit("setErrorCode", response.data.errorCode);
        if (response.data.errorCode === 0) {
          showSuccess(response.data.result)
        } else if (response.status === 422) {
          showErrors(response.data.errors)
        } else {
          showError(response.data.message)
        }
      })
  },
  //编辑
  dataEdit({ commit }, page) {
    commit("setErrorCode", false);
    request.post(Server.action.group_update, page)
      .then(response => {
        console.log(response);
        commit("setErrorCode", response.data.errorCode);
        if (response.data.errorCode === 0) {
          showSuccess(response.data.result)
        } else {
          showError(response.data.message)
        }
      })
  },
  // ID删除 > 参数为：url/id
  dataDel({ commit }, id) {
    request.get(Server.action.group_delete + id)
      .then(response => {
        console.log(response);
        if (response.data.errorCode === 0) {
          showSuccess(response.data.result)
        } else {
          showError(response.data.message)
        }
      })
  },
  // // 批量删除 > 参数为：[1,2,3,4,5,6]
  // dataDel({commit}, ids) {
  //   request.get(Server.action.group_delete ,{ params: ids })
  //   .then(response => {
  //     console.log(response);
  //     if (response.data.errorCode === 0) {
  //       showSuccess(response.data.result)
  //     } else {
  //       showError( response.data.message )
  //     }
  //   })
  // },
  // 详情
  dataDetail({ commit }, id) {
    request.get(Server.action.group_detail + id)
      .then(response => {
        console.log(response);
        if (response.data.errorCode === 0) {
          commit("setDetail", response.data.result);
          // showSuccess(response.data.result)
        } else {
          showError(response.data.message)
        }
      })
  },
  // 设备分组信息
  groupAll({ commit }) {
    request.get(Server.action.group_all)
      .then(response => {
        if (response.data.errorCode === 0) {
          // /* 去除3级 */
          // let groupArr = response.data.result
          // groupArr.forEach((items, i)=>{
          //   items.parent.forEach((item, i)=>{
          //     // item.parent = []
          //     delete item.parent
          //   })
          // })
          commit("setGroupAll", response.data.result);
        } else {
          showError(response.data.message)
        }
      })
  },
  // 设备分组信息(套餐页面)
  groupDataAll({ commit }, type) {
    request.get(Server.action.group_all)
      .then(response => {
        if (response.data.errorCode === 0) {
          let groupArr = response.data.result
          if (type == 2) {//区域代理
            groupArr.forEach(items => {
              delete items.parent
            })
          } else if (type == 3) {//单位
            groupArr.forEach(items => {
              items.parent.forEach(item => {
                delete item.parent
              })
            })
          }
          commit("setGroupAll", groupArr);
        } else {
          showError(response.data.message)
        }
      })
  },
  // 设备分组信息(注册页面)
  registerGroupAll({ commit }) {
    request.get(Server.action.passport_group)
      .then(response => {
        if (response.data.errorCode === 0) {
          commit("setGroupAll", response.data.result);
        } else {
          showError(response.data.message)
        }
      })
  },

  /* ~~2.0公司分组管理~~ */

  // 平台管理列表
  getPlatFormList({ commit }, page) {
    request.get(Server.action.userPlatform_list, { params: page })
      .then(response => {
        console.log(response);
        if (response.data.errorCode === 0) {
          commit("setPlatFormLists", response.data.result.list);
          let fields = response.data.result.fields;
          fields.push({
            title: '操作',
            align: 'center',
            width: 320,
            scopedSlots: { customRender: 'action' },
            fixed: 'right',
          })
          fields[0].width = 80
          fields.forEach(function (item, i) {
            fields[i].dataIndex = fields[i].key
            fields[i].align = 'center';
            if (i < fields.length - 2) {
              item.width = item.width || 150
            }
          })
          commit("setPlatFormFields", fields);
        } else {

        }
      })
  },
  // 代理管理列表
  getAgentList({ commit }, page) {
    request.get(Server.action.agent_list, { params: page })
      .then(response => {
        console.log(response);
        if (response.data.errorCode === 0) {
          commit("setAgentLists", response.data.result.list);
          let fields = response.data.result.fields;
          fields.push({
            title: '操作',
            align: 'center',
            width: 200,
            scopedSlots: { customRender: 'action' },
            fixed: 'right',
          })
          fields[0].width = 80
          fields.forEach(function (item, i) {
            fields[i].dataIndex = fields[i].key
            fields[i].align = 'center';
            if (i < fields.length - 2) {
              item.width = item.width || 150
            }
          })
          commit("setAgentFields", fields);
        } else {

        }
      })
  },
  // 公司管理列表
  getCompanyList({ commit }, page) {
    request.get(Server.action.company_list, { params: page })
      .then(response => {
        console.log(response);
        if (response.data.errorCode === 0) {
          commit("setCompanyLists", response.data.result.list);
          let fields = response.data.result.fields;
          fields.push({
            title: '操作',
            align: 'center',
            width: 160,
            scopedSlots: { customRender: 'action' },
            fixed: 'right',
          })
          fields[0].width = 80
          fields.forEach(function (item, i) {
            fields[i].dataIndex = fields[i].key
            fields[i].align = 'center';
            if (i < fields.length - 2) {
              item.width = item.width || 150
            }
          })
          commit("setCompanyFields", fields);
        } else {

        }
      })
  },
  // 平台分配代理
  agentConfig({ commit }, data) {
    console.log(data);
    commit("setErrorCode", false);
    request.post(Server.action.userPlatform_distributionAgent, data)
      .then(response => {
        console.log(response);
        commit("setErrorCode", response.data.errorCode);
        if (response.data.errorCode === 0) {
          showSuccess(response.data.result)
        } else {
          showError(response.data.message)
        }
      })
  },
  // 平台分配公司
  companConfig({ commit }, data) {
    commit("setErrorCode", false);
    request.post(Server.action.userPlatform_distributionCompany, data)
      .then(response => {
        console.log(response);
        commit("setErrorCode", response.data.errorCode);
        if (response.data.errorCode === 0) {
          showSuccess(response.data.result)
        } else {
          showError(response.data.message)
        }
      })
  },
  //代理分配公司
  agentConfigCompan({ commit }, data) {
    commit("setErrorCode", false);
    request.post(Server.action.agent_distributionCompany, data)
      .then(response => {
        console.log(response);
        commit("setErrorCode", response.data.errorCode);
        if (response.data.errorCode === 0) {
          showSuccess(response.data.result)
        } else {
          showError(response.data.message)
        }
      })
  },
  // 平台管理删除
  PlatFormDel({ commit }, ids) {
    request.get(Server.action.userPlatform_delete, { params: ids })
      .then(response => {
        console.log(response);
        if (response.data.errorCode === 0) {
          showSuccess(response.data.result)
        } else {
          showError(response.data.message)
        }
      })
  },
  //代理组添加
  agentAdd({ commit }, formData) {
    commit("setErrorCode", false);
    if(formData.name == '') {
      showError( '代理组名称不能为空' )
      return false
    }
    request.post(Server.action.agent_add, formData)
      .then(response => {
        console.log(response);
        commit("setErrorCode", response.data.errorCode);
        if (response.data.errorCode === 0) {
          showSuccess(response.data.result.msg)
        } else if (response.status === 422) {
          showErrors(response.data.errors)
        } else {
          showError(response.data.message)
        }
      })
  },
  // 代理组删除
  agentDel({ commit }, ids) {
    request.get(Server.action.agent_delete, { params: ids })
      .then(response => {
        console.log(response);
        if (response.data.errorCode === 0) {
          showSuccess(response.data.result)
        } else {
          showError(response.data.message)
        }
      })
  },
  //代理组编辑
  agentEdit({ commit }, formData) {
    commit("setErrorCode", false);
    if(formData.name == '') {
      showError( '公司名称不能为空' )
      return false
    }
    request.post(Server.action.agent_update, formData)
      .then(response => {
        console.log(response);
        commit("setErrorCode", response.data.errorCode);
        if (response.data.errorCode === 0) {
          showSuccess(response.data.result)
        } else {
          showError(response.data.message)
        }
      })
  },
  
  //公司添加
  companyAdd({ commit }, formData) {
    commit("setErrorCode", false);
    if(formData.name == '') {
      showError( '公司名称不能为空' )
      return false
    }
    request.post(Server.action.company_add, formData)
      .then(response => {
        console.log(response);
        commit("setErrorCode", response.data.errorCode);
        if (response.data.errorCode === 0) {
          showSuccess(response.data.result.msg)
        } else if (response.status === 422) {
          showErrors(response.data.errors)
        } else {
          showError(response.data.message)
        }
      })
  },
  // 公司删除
  companyDel({ commit }, ids) {
    request.get(Server.action.agent_delete, { params: ids })
      .then(response => {
        console.log(response);
        if (response.data.errorCode === 0) {
          showSuccess(response.data.result)
        } else {
          showError(response.data.message)
        }
      })
  },
  //公司编辑
  companyEdit({ commit }, formData) {
    commit("setErrorCode", false);
    if(formData.name == '') {
      showError( '公司名称不能为空' )
      return false
    }
    request.post(Server.action.agent_update, formData)
      .then(response => {
        console.log(response);
        commit("setErrorCode", response.data.errorCode);
        if (response.data.errorCode === 0) {
          showSuccess(response.data.result)
        } else {
          showError(response.data.message)
        }
      })
  },
  // 平台All
  getUserPlatformAll({ commit }) {
    request.get(Server.action.userPlatform_all)
      .then(response => {
        if (response.data.errorCode === 0) {
          console.log('userPlatformAll', response);
          let data = response.data.result
          // data.map(item => {
          //   item.key = String(item.id)
          //   item.title = item.name
          // })
          commit("setUserPlatformAll", data);
        } else {
          showError(response.data.message)
        }
      })
  },
  // 代理All
  agentAll({ commit }) {
    request.get(Server.action.agent_all)
      .then(response => {
        if (response.data.errorCode === 0) {
          console.log('agentAll', response);
          let data = response.data.result
          data.map(item => {
            item.key = String(item.id)
            item.title = item.name
          })
          commit("setAgentAll", data);
        }
      })
  },
  // 公司all
  companyAll({ commit }) {
    request.get(Server.action.company_all)
      .then(response => {
        if (response.data.errorCode === 0) {
          console.log('companyAll', response);
          let data = response.data.result
          data.map(item => {
            item.key = String(item.id)
            item.title = item.name
          })
          commit("setCompanyAll", data);
        }
      })
  },
};

const mutations = {
  setErrorCode(state, text) {
    state.requestCode = text;
  },
  setLists(state, lists) {
    state.dataLists = lists;
  },
  setFields(state, lists) {
    state.Fields = lists;
  },
  setDetail(state, data) {
    state.itemDetail = data;
  },
  setGroupAll(state, data) {
    state.groupData = data;
  },
  setPlatFormLists(state, lists) {
    state.dataPlatFormLists = lists;
  },
  setPlatFormFields(state, lists) {
    state.PlatFormFields = lists;
  },
  setAgentLists(state, lists) {
    state.dataAgentLists = lists;
  },
  setAgentFields(state, lists) {
    state.AgentFields = lists;
  },
  setCompanyLists(state, lists) {
    state.dataCompanyLists = lists;
  },
  setCompanyFields(state, lists) {
    state.CompanyFields = lists;
  },
  setUserPlatformAll(state, lists) {
    state.userPlatformAll = lists;
  },
  setAgentAll(state, lists) {
    state.agentAllData = lists;
  },
  setCompanyAll(state, lists) {
    state.companyAllData = lists;
  },
};
export default {
  namespaced: true,
  state,
  actions,
  mutations
};
